﻿using Microsoft.Extensions.DependencyInjection;
using Polly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using XjjXmm.FrameWork.ToolKit;

namespace XjjXmm.Spider.biquge.BiqoogeCom;


internal class SetUpProfile : AbstractSetUpProfile<BiqoogeComSpider>
{
    //public static readonly string domain = Setting.FindSetting<BiqoogeComSpider>().Domain;
    //public static readonly List<string> keys = Setting.GetKeys<BiqoogeComSpider>();


    private HttpClientHandler BuildHttpClientHandler()
    {
        return new HttpClientHandler()
        {
            AutomaticDecompression = System.Net.DecompressionMethods.All,
        };
    }

    protected override void ConfigClient(string key, IServiceCollection services, string? proxy = null)
    {
        var httpClientHandler = BuildHttpClientHandler();
        if (!string.IsNullOrEmpty(proxy))
        {
            httpClientHandler.Proxy = new WebProxy(proxy);
            httpClientHandler.UseProxy = true;
        }


        var httpClientBuild = services.AddHttpClient(key, client =>
        {
            client.BaseAddress = new Uri(domain);

            client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
        })
         .AddHttpMessageHandler<HttpLogHandel<BiqoogeComSpider>>()
         .AddHttpMessageHandler<HttpGBKHandel>()
         .AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(60)))
        .ConfigurePrimaryHttpMessageHandler(() => httpClientHandler);
    }


}
